using System;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace JMClient.Assets.ClientDesignConfig;

/// <summary>
/// Avalonia 窗口配置类
/// </summary>
[JsonSerializable(typeof(AvaloniaWindowConfig))]
public class AvaloniaWindowConfig
{
    /// <summary>
    /// 窗口尺寸配置
    /// </summary>
    [JsonPropertyName("size")]
    public WindowSizeConfig Size { get; set; } = new();
    
    /// <summary>
    /// 窗口行为配置
    /// </summary>
    [JsonPropertyName("behavior")]
    public WindowBehaviorConfig Behavior { get; set; } = new();
    
    /// <summary>
    /// 窗口样式配置
    /// </summary>
    [JsonPropertyName("style")]
    public WindowStyleConfig Style { get; set; } = new();
    
    /// <summary>
    /// 窗口位置配置
    /// </summary>
    [JsonPropertyName("position")]
    public WindowPositionConfig Position { get; set; } = new();
    
    /// <summary>
    /// 配置元数据
    /// </summary>
    [JsonPropertyName("metadata")]
    public WindowMetadataConfig Metadata { get; set; } = new();
}

/// <summary>
/// 窗口尺寸配置
/// </summary>
[JsonSerializable(typeof(WindowSizeConfig))]
public class WindowSizeConfig
{
    /// <summary>
    /// 窗口宽度
    /// </summary>
    [JsonPropertyName("width")]
    [Range(100, 10000)]
    public double Width { get; set; } = 1200;
    
    /// <summary>
    /// 窗口高度
    /// </summary>
    [JsonPropertyName("height")]
    [Range(100, 10000)]
    public double Height { get; set; } = 800;
    
    /// <summary>
    /// 最小宽度
    /// </summary>
    [JsonPropertyName("minWidth")]
    [Range(100, 10000)]
    public double MinWidth { get; set; } = 800;
    
    /// <summary>
    /// 最小高度
    /// </summary>
    [JsonPropertyName("minHeight")]
    [Range(100, 10000)]
    public double MinHeight { get; set; } = 600;
    
    /// <summary>
    /// 最大宽度（0表示无限制）
    /// </summary>
    [JsonPropertyName("maxWidth")]
    [Range(0, 10000)]
    public double MaxWidth { get; set; } = 0;
    
    /// <summary>
    /// 最大高度（0表示无限制）
    /// </summary>
    [JsonPropertyName("maxHeight")]
    [Range(0, 10000)]
    public double MaxHeight { get; set; } = 0;
    
    /// <summary>
    /// 内容自适应模式
    /// </summary>
    [JsonPropertyName("sizeToContent")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public SizeToContent SizeToContent { get; set; } = SizeToContent.Manual;
}

/// <summary>
/// 窗口行为配置
/// </summary>
[JsonSerializable(typeof(WindowBehaviorConfig))]
public class WindowBehaviorConfig
{
    /// <summary>
    /// 窗口标题
    /// </summary>
    [JsonPropertyName("title")]
    [StringLength(200)]
    public string Title { get; set; } = "JM Client";
    
    /// <summary>
    /// 启动位置
    /// </summary>
    [JsonPropertyName("startupLocation")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public WindowStartupLocation StartupLocation { get; set; } = WindowStartupLocation.CenterScreen;
    
    /// <summary>
    /// 窗口状态
    /// </summary>
    [JsonPropertyName("windowState")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public WindowState WindowState { get; set; } = WindowState.Normal;
    
    /// <summary>
    /// 调整大小模式
    /// </summary>
    [JsonPropertyName("resizeMode")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public ResizeMode ResizeMode { get; set; } = ResizeMode.CanResize;
    
    /// <summary>
    /// 是否可调整大小
    /// </summary>
    [JsonPropertyName("canResize")]
    public bool CanResize { get; set; } = true;
    
    /// <summary>
    /// 是否可最小化
    /// </summary>
    [JsonPropertyName("canMinimize")]
    public bool CanMinimize { get; set; } = true;
    
    /// <summary>
    /// 是否可最大化
    /// </summary>
    [JsonPropertyName("canMaximize")]
    public bool CanMaximize { get; set; } = true;
    
    /// <summary>
    /// 是否在任务栏显示
    /// </summary>
    [JsonPropertyName("showInTaskbar")]
    public bool ShowInTaskbar { get; set; } = true;
    
    /// <summary>
    /// 是否激活显示
    /// </summary>
    [JsonPropertyName("showActivated")]
    public bool ShowActivated { get; set; } = true;
    
    /// <summary>
    /// 是否置顶
    /// </summary>
    [JsonPropertyName("topmost")]
    public bool Topmost { get; set; } = false;
}

/// <summary>
/// 窗口样式配置
/// </summary>
[JsonSerializable(typeof(WindowStyleConfig))]
public class WindowStyleConfig
{
    /// <summary>
    /// 系统装饰
    /// </summary>
    [JsonPropertyName("systemDecorations")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public SystemDecorations SystemDecorations { get; set; } = SystemDecorations.Full;
    
    /// <summary>
    /// 透明度
    /// </summary>
    [JsonPropertyName("opacity")]
    [Range(0.1, 1.0)]
    public double Opacity { get; set; } = 1.0;
    
    /// <summary>
    /// 背景色
    /// </summary>
    [JsonPropertyName("background")]
    [RegularExpression(@"^#[0-9A-Fa-f]{6}$")]
    public string Background { get; set; } = "#FFFFFF";
    
    /// <summary>
    /// 是否扩展客户区到装饰区域
    /// </summary>
    [JsonPropertyName("extendClientAreaToDecorationsHint")]
    public bool ExtendClientAreaToDecorationsHint { get; set; } = false;
    
    /// <summary>
    /// 扩展客户区铬提示
    /// </summary>
    [JsonPropertyName("extendClientAreaChromeHints")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public ExtendClientAreaChromeHints ExtendClientAreaChromeHints { get; set; } = ExtendClientAreaChromeHints.None;
    
    /// <summary>
    /// 扩展客户区标题栏高度提示
    /// </summary>
    [JsonPropertyName("extendClientAreaTitleBarHeightHint")]
    [Range(0, 100)]
    public double ExtendClientAreaTitleBarHeightHint { get; set; } = 0;
    
    /// <summary>
    /// 透明度级别提示
    /// </summary>
    [JsonPropertyName("transparencyLevelHint")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public WindowTransparencyLevel TransparencyLevelHint { get; set; } = WindowTransparencyLevel.None;
}

/// <summary>
/// 窗口位置配置
/// </summary>
[JsonSerializable(typeof(WindowPositionConfig))]
public class WindowPositionConfig
{
    /// <summary>
    /// 上次位置
    /// </summary>
    [JsonPropertyName("lastPosition")]
    public WindowPoint LastPosition { get; set; } = new();
    
    /// <summary>
    /// 是否记忆位置
    /// </summary>
    [JsonPropertyName("rememberPosition")]
    public bool RememberPosition { get; set; } = true;
    
    /// <summary>
    /// 是否记忆尺寸
    /// </summary>
    [JsonPropertyName("rememberSize")]
    public bool RememberSize { get; set; } = true;
    
    /// <summary>
    /// 是否记忆状态
    /// </summary>
    [JsonPropertyName("rememberState")]
    public bool RememberState { get; set; } = true;
    
    /// <summary>
    /// 是否约束到屏幕
    /// </summary>
    [JsonPropertyName("constrainToScreen")]
    public bool ConstrainToScreen { get; set; } = true;
    
    /// <summary>
    /// 是否约束到工作区
    /// </summary>
    [JsonPropertyName("constrainToWorkArea")]
    public bool ConstrainToWorkArea { get; set; } = true;
}

/// <summary>
/// 窗口位置点
/// </summary>
[JsonSerializable(typeof(WindowPoint))]
public class WindowPoint
{
    /// <summary>
    /// X 坐标
    /// </summary>
    [JsonPropertyName("x")]
    [Range(-10000, 10000)]
    public int X { get; set; } = 100;
    
    /// <summary>
    /// Y 坐标
    /// </summary>
    [JsonPropertyName("y")]
    [Range(-10000, 10000)]
    public int Y { get; set; } = 100;
}

/// <summary>
/// 窗口配置元数据
/// </summary>
[JsonSerializable(typeof(WindowMetadataConfig))]
public class WindowMetadataConfig
{
    /// <summary>
    /// 配置版本
    /// </summary>
    [JsonPropertyName("configVersion")]
    [Range(1, 999)]
    public int ConfigVersion { get; set; } = 1;
    
    /// <summary>
    /// 最后修改时间
    /// </summary>
    [JsonPropertyName("lastModified")]
    [JsonConverter(typeof(JsonStringEnumConverter))]
    public DateTime LastModified { get; set; } = DateTime.Now;
    
    /// <summary>
    /// 配置描述
    /// </summary>
    [JsonPropertyName("description")]
    [StringLength(500)]
    public string Description { get; set; } = "Avalonia Window Configuration";
    
    /// <summary>
    /// 是否启用配置
    /// </summary>
    [JsonPropertyName("isEnabled")]
    public bool IsEnabled { get; set; } = true;
}

#region 枚举定义

/// <summary>
/// 内容自适应模式枚举
/// </summary>
public enum SizeToContent
{
    /// <summary>
    /// 手动调整
    /// </summary>
    Manual,
    
    /// <summary>
    /// 宽度自适应
    /// </summary>
    Width,
    
    /// <summary>
    /// 高度自适应
    /// </summary>
    Height,
    
    /// <summary>
    /// 宽高自适应
    /// </summary>
    WidthAndHeight
}

/// <summary>
/// 窗口启动位置枚举
/// </summary>
public enum WindowStartupLocation
{
    /// <summary>
    /// 手动指定
    /// </summary>
    Manual,
    
    /// <summary>
    /// 屏幕中央
    /// </summary>
    CenterScreen,
    
    /// <summary>
    /// 所有者窗口中央
    /// </summary>
    CenterOwner
}

/// <summary>
/// 窗口状态枚举
/// </summary>
public enum WindowState
{
    /// <summary>
    /// 正常状态
    /// </summary>
    Normal,
    
    /// <summary>
    /// 最小化
    /// </summary>
    Minimized,
    
    /// <summary>
    /// 最大化
    /// </summary>
    Maximized,
    
    /// <summary>
    /// 全屏
    /// </summary>
    FullScreen
}

/// <summary>
/// 调整大小模式枚举
/// </summary>
public enum ResizeMode
{
    /// <summary>
    /// 不可调整大小
    /// </summary>
    NoResize,
    
    /// <summary>
    /// 仅可最小化
    /// </summary>
    CanMinimize,
    
    /// <summary>
    /// 可调整大小
    /// </summary>
    CanResize,
    
    /// <summary>
    /// 可调整大小（带手柄）
    /// </summary>
    CanResizeWithGrip
}

/// <summary>
/// 系统装饰枚举
/// </summary>
public enum SystemDecorations
{
    /// <summary>
    /// 无装饰
    /// </summary>
    None,
    
    /// <summary>
    /// 仅边框
    /// </summary>
    BorderOnly,
    
    /// <summary>
    /// 完整装饰
    /// </summary>
    Full
}

/// <summary>
/// 扩展客户区铬提示枚举
/// </summary>
public enum ExtendClientAreaChromeHints
{
    /// <summary>
    /// 无提示
    /// </summary>
    None,
    
    /// <summary>
    /// 默认
    /// </summary>
    Default,
    
    /// <summary>
    /// 预调整大小
    /// </summary>
    PreferSystemChrome
}

/// <summary>
/// 窗口透明度级别枚举
/// </summary>
public enum WindowTransparencyLevel
{
    /// <summary>
    /// 无透明度
    /// </summary>
    None,
    
    /// <summary>
    /// 透明
    /// </summary>
    Transparent,
    
    /// <summary>
    /// 模糊
    /// </summary>
    Blur,
    
    /// <summary>
    /// 亚克力模糊
    /// </summary>
    AcrylicBlur
}

#endregion 